package com.dlwx.parttimetalent.fragment.daren;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.MyBasicInfoBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.DialogInter;
import com.dlwx.parttimetalent.ui.IdentityActivity;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.views.EducationDialog;
import com.dlwx.parttimetalent.views.MarryDialog;
import com.dlwx.parttimetalent.views.MyProgressLoading;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class BasicInfoFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.et_nick)
    EditText etNick;
    @BindView(R.id.et_name)
    TextView etName;
    @BindView(R.id.et_phone)
    TextView etPhone;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.et_age)
    EditText etAge;
    @BindView(R.id.et_marry)
    TextView etMarry;
    @BindView(R.id.et_height)
    TextView etHeight;
    @BindView(R.id.et_weight)
    TextView etWeight;
    @BindView(R.id.tv_education)
    TextView tvEducation;
    @BindView(R.id.et_major)
    EditText etMajor;
    @BindView(R.id.et_advantage)
    EditText etAdvantage;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    Unbinder unbinder;
    @BindView(R.id.ll_height)
    LinearLayout llHeight;
    @BindView(R.id.ll_weight)
    LinearLayout llWeight;

    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;
    private MyProgressLoading loadingDialog;

    public BasicInfoFragment() {

    }


    public static BasicInfoFragment newInstance(String param1, String param2) {
        BasicInfoFragment fragment = new BasicInfoFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = new MyProgressLoading(getActivity(), R.style.DialogStyle);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_basic_info, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        getDatas();
        return view;
    }

    private void initView() {

    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.post(Http.MyBasicInfo, getActivity())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        MyBasicInfoBean parentBean = DealResult.getInstance().dealDatas(getActivity(), response, new TypeToken<MyBasicInfoBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        setDatas(parentBean.getBody());
//                        ToastUtils.toastForShort(getActivity(), parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
                    }
                });
    }

    private void setDatas(MyBasicInfoBean.BodyBean bean) {
        if (bean == null) {
            return;
        }
        if (1 == bean.getSex()) {
            tvSex.setText("男");
        } else {
            tvSex.setText("女");
        }
        etAge.setText(bean.getAge() + "");
        etMarry.setText(bean.getMarriage());
        etHeight.setText(bean.getHeight());
        etWeight.setText(bean.getWeight());
        tvEducation.setText(bean.getEducation());
        etMajor.setText(bean.getMajor());
        etMajor.setSelection(etMajor.getText().length());
        etAdvantage.setText(bean.getSpecialty());
        etAdvantage.setSelection(etAdvantage.getText().length());
        etNick.setText(bean.getNickname());
        etNick.setSelection(etNick.getText().length());
        etName.setText(bean.getUsername());
        etPhone.setText(bean.getPhone_no());
    }


    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
//            throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_sex, R.id.tv_education, R.id.tv_confirm, R.id.et_phone, R.id.et_marry, R.id.et_name,R.id.ll_height,R.id.ll_weight})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_sex:
//                SexDialog dialog = new SexDialog(getActivity());
//                dialog.show();
                break;
            case R.id.et_marry:
                String[] times = new String[]{"已婚", "未婚", "离异", "丧偶"};
                MarryDialog dialog = new MarryDialog(getActivity(), times, new DialogInter() {
                    @Override
                    public void getInfo(String data) {
                        etMarry.setText(data);
                    }

                    @Override
                    public void getWorkInfo(String start, String end, String unit) {
                    }
                });
                dialog.show();
                break;
            case R.id.ll_height:
                List<String> hList=new ArrayList<>();
                for(int i=150;i<201;i++){
                    hList.add(i+"");
                }
                String[] heights=new String[hList.size()];
                hList.toArray(heights);
                MarryDialog dialog1 = new MarryDialog(getActivity(), heights, new DialogInter() {
                    @Override
                    public void getInfo(String data) {
                        etHeight.setText(data);
                    }

                    @Override
                    public void getWorkInfo(String start, String end, String unit) {
                    }
                });
                dialog1.show();
                break;
            case R.id.ll_weight:
                List<String> wList=new ArrayList<>();
                for(int i=30;i<101;i++){
                    wList.add(i+"");
                }
                String[] weights=new String[wList.size()];
                wList.toArray(weights);
                MarryDialog dialog2 = new MarryDialog(getActivity(), weights, new DialogInter() {
                    @Override
                    public void getInfo(String data) {
                        etWeight.setText(data);
                    }

                    @Override
                    public void getWorkInfo(String start, String end, String unit) {
                    }
                });
                dialog2.show();
                break;
            case R.id.tv_education:
                EducationDialog dialog3 = new EducationDialog(getActivity(), new DialogInter() {
                    @Override
                    public void getInfo(String data) {
                        tvEducation.setText(data);
                    }

                    @Override
                    public void getWorkInfo(String start, String end, String unit) {
                    }
                });
                dialog3.show();
                break;
            case R.id.tv_confirm:
                checkInputDatas();

                break;
            case R.id.et_phone:
//                startActivity(new Intent(getActivity(), ModifyPhoneActivity.class));
                break;
            case R.id.et_name:
                startActivity(new Intent(getActivity(), IdentityActivity.class));
                break;
        }
    }

    private void checkInputDatas() {
        String nickName = etNick.getText().toString();
        if (TextUtils.isEmpty(nickName)) {
            ToastUtils.toastForShort(getActivity(), "昵称不能为空");
            return;
        }
        String phone = etPhone.getText().toString();
//        if(TextUtils.isEmpty(phone)){
//            ToastUtils.toastForShort(getActivity(),"手机号码不能为空");    return;
//        }
        String age = etAge.getText().toString();
        if (TextUtils.isEmpty(age)) {
            ToastUtils.toastForShort(getActivity(), "年龄不能为空");
            return;
        }
        String marry = etMarry.getText().toString();
//        if(TextUtils.isEmpty(marry)){
//            ToastUtils.toastForShort(getActivity(),"婚姻状况不能为空"); return;
//        }
        String heightStr = etHeight.getText().toString();
//        if(TextUtils.isEmpty(heightStr)){
//            ToastUtils.toastForShort(getActivity(),"身高数据不能为空");    return;
//        }
        String weightStr = etWeight.getText().toString();
//        if(TextUtils.isEmpty(weightStr)){
//            ToastUtils.toastForShort(getActivity(),"体重数据不能为空");    return;
//        }
        String education = tvEducation.getText().toString();
//        if(TextUtils.isEmpty(education)){
//            ToastUtils.toastForShort(getActivity(),"学历不能为空");    return;
//        }
        String major = etMajor.getText().toString();
//        if(TextUtils.isEmpty(major)){
//            ToastUtils.toastForShort(getActivity(),"专业不能为空");    return;
//        }
        String advantage = etAdvantage.getText().toString();
//        if(TextUtils.isEmpty(advantage)){
//            ToastUtils.toastForShort(getActivity(),"特长不能为空");    return;
//        }
        double heighDouble = 0;
        double weightDouble = 0;
        if (heightStr.contains("cm")) {
            heightStr = heightStr.substring(0, heightStr.length() - 2);
        }
        if (weightStr.contains("kg")) {
            weightStr = weightStr.substring(0, weightStr.length() - 2);
        }
        try {
            heighDouble = Double.parseDouble(heightStr);
            weightDouble = Double.parseDouble(weightStr);
        } catch (Exception e) {
        }
        confirm(nickName, phone, age, marry, heighDouble, weightDouble, education, major, advantage);
    }

    private void confirm(String nickName, String phone, String age, String marry
            , double heightStr, double weightStr, String education, String major, String advantage) {
        OkgoUtil.post(Http.SubUserBasic, getActivity())
                .params("major", major)
                .params("nickname", nickName)
                .params("marriage", marry)
                .params("age", age)
                .params("height", heightStr)
                .params("weight", weightStr)
                .params("specialty", advantage)
                .params("education", education)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(getActivity(), response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                        }
                        ToastUtils.toastForShort(getActivity(), parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
                    }
                });
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
